package com.cgx.tablet.beeper;


import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.cgx.tablet.beeper.base.BaseActivity;
import com.cgx.tablet.beeper.database.DatabaseHelper;
import com.cgx.tablet.beeper.utils.CrashHandler;
import com.finddreams.languagelib.MultiLanguageUtil;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/10 0010.
 */

public class MyApplication extends MultiDexApplication{

    private static MyApplication application;

    public static String PHOTO_BASE_URL;

    // Activity列表
    private List<BaseActivity> mActivityList = new LinkedList<BaseActivity>();

    public static Context getGlobalContext() {
        return application.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MultiLanguageUtil.init(this);
        ScreenAdapterTools.init(this);


        application = this;
        //异常捕获
        CrashHandler.getInstance().init(this, getString(R.string.crashLogDir));
        //初始化Retrofit和API接口
//        ApiUtil.init(getString(R.string.service_domain, SharedPreferenceUtil.getIP(getGlobalContext()), SharedPreferenceUtil.getPort(getGlobalContext())));
        //初始化Realm
        DatabaseHelper.init(this);
    }

    //旋转适配,如果应用屏幕固定了某个方向不旋转的话(比如qq和微信),下面可不写.
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        ScreenAdapterTools.getInstance().reset(this);
    }

    /**
     * 添加activity到App的mActivityList
     *
     * @param activity
     */
    public void addActivity(BaseActivity activity) {
        this.mActivityList.add(activity);
    }

    /**
     * 遍历mActivityList，结束每一个activity的声明周期
     */
    private void finishActivityList() {
        for (Activity activity : mActivityList) {
            activity.finish();
        }
    }

    /**
     * 完全退出程序
     */
    public void appExit() {
        Log.e("MyApplication","DRApplication: appExit()");

        finishActivityList();
        // 正常退出
        System.exit(0);
    }
}
